/**
 * <p>Title: Music.java</p>
 * <p>Description: CS 343 Final Project. This class will allow for music during the game</p>
 * @author Tim Olmstead, Brady Fetting, Erik Brown, Thales Kirk
 * @email timothy.olmstead@my.uwrf.edu, brady.fetting@my.uwrf.edu, 
 * 			erik.brown@my.uwrf.edu, thales.kirk@my.uwrf.edu
 * @date November 15, 2013
 */

package Components;
import java.io.File;
import java.io.IOException;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;

public class Music  {
	private File soundFile;
	private AudioInputStream sound;
	private DataLine.Info info;
	private static Clip bgSound;
	private String dir = "src/Music/";
	private static boolean muted = false;
	
	public static void stopDaBeats(){
		bgSound.stop();
		bgSound.close();
	}

	public Music() {
		soundFile = null;
		sound = null;
		info = null;
		bgSound = null;

	}

	
	/**
	 * @exception IOException, Exception
	 * @description sets the background music while playing the game
	 */
	@SuppressWarnings("static-access")
	public void gamePLayMusic() throws IOException, Exception {
		soundFile = new File(dir+"gameBeat.wav");
		sound = AudioSystem.getAudioInputStream(soundFile);
		info = new DataLine.Info(Clip.class, sound.getFormat());
		bgSound = (Clip) AudioSystem.getLine(info);
		bgSound.open(sound);
		// play the sound clip
		if (bgSound.isActive())
			Thread.sleep(bgSound.getMicrosecondLength());
		bgSound.loop(bgSound.LOOP_CONTINUOUSLY);

	}
	
	/**
	 * @exception IOException, Exception
	 * @description sets the background music while in the main menu
	 */
	@SuppressWarnings("static-access")
	public void gameMainMenuMusic() throws IOException, Exception {
		soundFile = new File(dir+"mainloop.wav");
		sound = AudioSystem.getAudioInputStream(soundFile);
		info = new DataLine.Info(Clip.class, sound.getFormat());
		bgSound = (Clip) AudioSystem.getLine(info);
		bgSound.open(sound);
		// play the sound clip
		if (bgSound.isActive())
			Thread.sleep(bgSound.getMicrosecondLength());
		bgSound.loop(bgSound.LOOP_CONTINUOUSLY);

	}
	
	/**
	 * @param none
	 * @description returns if the music is playing
	 */
	public boolean isPlaying(){
		try{
		if (bgSound.isActive())
			return true;
		}catch(NullPointerException a){
			return false;
		}
		return false;
	}

	/**
	 * @param none
	 * @description returns if the game is muted
	 */
	public static boolean isMuted() {
		return muted;
	}

	/**
	 * @param none
	 * @description operates the mute switch in the gameF
	 */
	public static void switchMuted() {
		muted = !muted;
	}
}
